# MIT License
#
# Copyright (c) 2024-2025 milkpotatoes
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

include(${CMAKE_CURRENT_LIST_DIR}/utils.cmake)

if(ARK_REPO_PATH)
    get_filename_component(ARK_ABS_REPO_PATH ${ARK_REPO_PATH} ABSOLUTE)
else()
    set(REPO_PATH_RELEATIVE "..")
    get_filename_component(ARK_ABS_REPO_PATH ${REPO_PATH_RELEATIVE} ABSOLUTE)
endif()

set(TOOLCHAIN_PATH "${ARK_ABS_REPO_PATH}/prebuilts")

set(CLANG_PATH_RELATIVE "${TOOLCHAIN_PATH}/clang/ohos/linux-${CMAKE_HOST_SYSTEM_PROCESSOR}")
get_filename_component(CLANG_PATH ${CLANG_PATH_RELATIVE} ABSOLUTE)

if(NOT CMAKE_SYSTEM_NAME)
    set(CMAKE_SYSTEM_NAME OHOS)
endif()

set(CMAKE_AR "${CLANG_PATH}/llvm/bin/llvm-ar")
set(CMAKE_LINKER "${CLANG_PATH}/llvm/bin/llvm-ld")
set(CMAKE_NM "${CLANG_PATH}/llvm/bin/llvm-nm")
set(CMAKE_OBJDUMP "${CLANG_PATH}/llvm/bin/llvm-objdump")
set(CMAKE_RANLIB "${CLANG_PATH}/llvm/bin/llvm-ranlib")

if(CMAKE_BUILD_TYPE AND CMAKE_BUILD_TYPE STREQUAL Debug)
    set(ARK_BUILD_TYPE debug)
else()
    set(ARK_BUILD_TYPE release)
endif()

get_system_processor(CMAKE_SYSTEM_PROCESSOR "${OHOS_ARCH}")

if(NOT CLANG_PREFIX)
    if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
        set(ARK_BUILD_ARCH arm)
        set(CLANG_PREFIX armv7)
    elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
        set(ARK_BUILD_ARCH arm64)
        set(CLANG_PREFIX aarch64)
    elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
        set(ARK_BUILD_ARCH x64)
        set(CLANG_PREFIX x86_64)
    endif()
endif()

if(CLANG_PATH)
    set(CMAKE_CXX_COMPILER "${CLANG_PATH}/llvm/bin/${CLANG_PREFIX}-unknown-linux-ohos-clang++")
    set(CMAKE_C_COMPILER "${CLANG_PATH}/llvm/bin/${CLANG_PREFIX}-unknown-linux-ohos-clang")
endif()

if(CMAKE_SYSTEM_PROCESSOR)
    set(CMAKE_SYSROOT "${ARK_ABS_REPO_PATH}/out/${ARK_BUILD_ARCH}.${ARK_BUILD_TYPE}/obj/build/third_party_gn/musl")
    if (NOT EXISTS "${CMAKE_ROOT}")
        set(CMAKE_SYSROOT "${ARK_ABS_REPO_PATH}/out/${ARK_BUILD_ARCH}.${ARK_BUILD_TYPE}/obj/arkcompiler/toolchain/build/third_party_gn/musl")
    endif()
endif()

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
